
package xj.graph2d.app.project;

import static xj.app.BaseApplicationFrame.getResourceString;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.text.MessageFormat;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import xj.graph2d.Document;
import xj.graph2d.SimpleDrawDocument;
import xj.graph2d.app.DrawAction;
import xj.graph2d.app.DrawApp;
import xj.graph2d.dialog.DialogPanel;
import xj.graph2d.dialog.TitledDialogPanel;

public class NewProjectItemListener 
  extends DrawAction 
  implements ActionListener {

  class ProjectItemOptionPanel 
    extends DialogPanel {

    public ProjectItemOptionPanel(String name) {
      super(name);
      JLabel nameLabel = new JLabel(getResourceString("ProjectItemName.text") + " ");
      nameField = new JTextField();

      JLabel authorLabel = new JLabel(getResourceString("Author.text") + " ");
      authorField = new JTextField();
      JLabel titleLabel = new JLabel(getResourceString("Title.text") + " ");
      titleField = new JTextField();
      JLabel orgLabel = new JLabel(getResourceString("Organization.text") + " ");
      orgField = new JTextField();

      JLabel descriptionLabel = new JLabel(getResourceString("Description.text")  + " ");
      descriptionLabel.setVerticalAlignment(SwingConstants.TOP);
      descriptionField = new JTextArea(4, 40);
      descriptionField.setBorder(BorderFactory.createEtchedBorder());

      JLabel typeLabel = new JLabel(getResourceString("ProjectItemType.text") + " ");
      typeField = new JComboBox();
      for (int i = 0; i < Document.DocumentTypeNames.length; i++) {
	String display = 
	  getResourceString("DiagramType." + 
					    Document.DocumentTypeNames[i] + ".text");
	Document.DocumentType type = Document.toDocumentType(Document.DocumentTypeNames[i]);

	typeField.addItem(new DocTypeItem(display, type));
      }
      typeField.addItemListener(new ItemListener() {
	  public void itemStateChanged(ItemEvent e) {
	    if (e != null && 
		e.getStateChange() == ItemEvent.SELECTED) { 
	      setOption();
	    }
	  }
	});

      addRow(nameLabel, nameField);
      addRow(typeLabel, typeField);
      addRow(titleLabel, titleField);
      addRow(authorLabel, authorField);
      addRow(orgLabel, orgField);
      addRow(descriptionLabel, new JScrollPane(descriptionField));

      setPreferredSize(new Dimension(400, 330));
      //setOption();
    }

    @Override
    public void setOption() {
      if (parent == null) {
	ProjectManager projectManager = main.getProjectManager();
	ProjectItem cur = projectManager.getCurrentItem();
	if (cur != null) { 
	  if (cur instanceof ProjectFolder) { 
	    parent = (ProjectFolder) cur;
	  } else { 
	    parent = cur.getParent();
	  }
	}
	if (parent == null) {
	  parent = projectManager.getCurrentProject();
	}
      }
      DocTypeItem item = (DocTypeItem) typeField.getSelectedItem();
      Document.DocumentType type = item.type; 
      String newname = Document.getDefaultFilename(type);
      String ext = "." + main.getDocumentExtension(type);
      newname = parent.getNewItemName(newname, ext);
      nameField.setText(newname);
    }

    @Override
    public void applyOption() {
      String name = nameField.getText();
      if (name != null) {
	name = name.trim();
      }
      DocTypeItem item = (DocTypeItem) typeField.getSelectedItem();
      Document.DocumentType type = item.type; 

      boolean exists = false; 
      if (name != null && 
	  parent != null) {
	String path = parent.getFullPath();
	String filename = path + File.separator + name;
	String ext = "." + main.getDocumentExtension(type);
	File file = new File(filename);
	exists = file.exists();
      }

      if (exists) { 
	String title, message;
	title = getResourceString("NewItem.Error.title");
	message = getResourceString("NewItem.Error.message");
	message = MessageFormat.format(message, name, parent.getName());
	JOptionPane.showMessageDialog(main.getJFrame(), 
				      message, title, 
				      JOptionPane.ERROR_MESSAGE);
      } else { 
	main.newProjectItem(parent, name, type);
	SimpleDrawDocument doc = main.getDoc();
	if (doc != null) {
	  String title = titleField.getText();
	  String author = authorField.getText();
	  String org = orgField.getText();
	  String des = descriptionField.getText();
	  doc.setAuthor(author);
	  doc.setTitle(title);
	  doc.setOrganization(org);
	  doc.setDescription(des);
	}
      }
    }

    protected JTextField nameField;

    protected JComboBox typeField;

    protected JTextField authorField;

    protected JTextField titleField;

    protected JTextField orgField;

    protected JTextArea descriptionField;
  }

  
  protected TitledDialogPanel projectItemOptionDialog = null;
  protected ProjectItemOptionPanel projectItemPanel = null;
  
  protected ProjectFolder parent;
    
  public void setParent(ProjectFolder parent) {
    this.parent = parent;
  }

  public void actionPerformed(ActionEvent e) {

    if (projectItemOptionDialog == null) {
      //String newProjectItemPanelTitle = getResourceString("Item.title");
      String newProjectItemDialogTitle = getResourceString("NewProjectItemDialog.title");
      String newProjectItemHeader = getResourceString("NewProjectItemDialog.header");
      String[] newProjectItemText = { 
	getResourceString("NewProjectItemDialog.text1"),
	getResourceString("NewProjectItemDialog.text2"),	
      };
      Icon icon = DrawApp.getIcon("NewWizIcon");

      DialogPanel[] options = { 
	projectItemPanel = new ProjectItemOptionPanel(null), //newProjectItemPanelTitle),
      };

      projectItemOptionDialog = new TitledDialogPanel(main.getJFrame(), 
						      JOptionPane.OK_CANCEL_OPTION, 
						      null, options);
      projectItemOptionDialog.setTitle(newProjectItemDialogTitle);
      projectItemOptionDialog.setTitlePaneText(newProjectItemHeader, newProjectItemText, icon);
    }

    projectItemOptionDialog.setOptions();
    projectItemOptionDialog.showOptionDialog();
  }

}
